package cn.edu.zafu.bigdata.x.xiejunjie.networkprogramming;

import java.io.*;
import java.net.*;
import java.util.Scanner;

public class SimpleTCPClient {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("localhost", 8080); // 连接到服务端
        System.out.println("已连接至服务端");

        Scanner scanner = new Scanner(System.in);
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

        while (true) {
            System.out.print("请输入消息: ");
            String message = scanner.nextLine();
            out.println(message); // 发送消息

            if (message.equalsIgnoreCase("exit")) break; // 如果输入exit则退出

            String response = in.readLine(); // 读取服务端响应
            System.out.println("服务端回复: " + response);
        }

        scanner.close();
        in.close();
        out.close();
        socket.close();
    }
}

